import { ElMessage, ElMessageBox, ElNotification } from 'element-plus'

// 全局消息提示工具
export class MessageUtil {
  // 成功消息
  static success(message, duration = 3000) {
    ElMessage({
      message,
      type: 'success',
      duration,
      showClose: true
    })
  }

  // 错误消息
  static error(message, duration = 5000) {
    ElMessage({
      message,
      type: 'error',
      duration,
      showClose: true
    })
  }

  // 警告消息
  static warning(message, duration = 4000) {
    ElMessage({
      message,
      type: 'warning',
      duration,
      showClose: true
    })
  }

  // 信息消息
  static info(message, duration = 3000) {
    ElMessage({
      message,
      type: 'info',
      duration,
      showClose: true
    })
  }

  // 确认对话框
  static confirm(message, title = '确认', options = {}) {
    return ElMessageBox.confirm(message, title, {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
      ...options
    })
  }

  // 输入对话框
  static prompt(message, title = '输入', options = {}) {
    return ElMessageBox.prompt(message, title, {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      ...options
    })
  }

  // 通知
  static notification(title, message, type = 'info', duration = 4500) {
    ElNotification({
      title,
      message,
      type,
      duration,
      position: 'top-right'
    })
  }

  // 加载中消息
  static loading(message = '加载中...') {
    return ElMessage({
      message,
      type: 'info',
      duration: 0,
      icon: 'Loading',
      showClose: false
    })
  }
}

// 文件工具类
export class FileUtil {
  // 检查文件类型
  static isImage(file) {
    return file.type.startsWith('image/')
  }

  // 检查文件大小
  static checkSize(file, maxSize = 50) {
    return file.size / 1024 / 1024 <= maxSize
  }

  // 读取文件为Base64
  static readAsDataURL(file) {
    return new Promise((resolve, reject) => {
      const reader = new FileReader()
      reader.onload = e => resolve(e.target.result)
      reader.onerror = reject
      reader.readAsDataURL(file)
    })
  }

  // 下载文件
  static download(content, filename, type = 'text/plain') {
    const blob = new Blob([content], { type })
    const url = URL.createObjectURL(blob)
    const a = document.createElement('a')
    a.href = url
    a.download = filename
    a.click()
    URL.revokeObjectURL(url)
  }
}

// 数学计算工具类
export class MathUtil {
  // 计算两点间距离
  static distance(p1, p2) {
    return Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2))
  }

  // 计算三点构成的角度
  static angle(p1, p2, p3) {
    const v1 = { x: p1.x - p2.x, y: p1.y - p2.y }
    const v2 = { x: p3.x - p2.x, y: p3.y - p2.y }

    const dot = v1.x * v2.x + v1.y * v2.y
    const mag1 = Math.sqrt(v1.x * v1.x + v1.y * v1.y)
    const mag2 = Math.sqrt(v2.x * v2.x + v2.y * v2.y)

    const angle = Math.acos(dot / (mag1 * mag2)) * (180 / Math.PI)
    return Math.round(angle * 10) / 10
  }

  // 线性插值
  static lerp(start, end, t) {
    return start + (end - start) * t
  }

  // 限制数值范围
  static clamp(value, min, max) {
    return Math.min(Math.max(value, min), max)
  }
}

// 本地存储工具类
export class StorageUtil {
  // 设置localStorage
  static setLocal(key, value) {
    try {
      localStorage.setItem(key, JSON.stringify(value))
    } catch (error) {
      console.error('localStorage设置失败:', error)
    }
  }

  // 获取localStorage
  static getLocal(key, defaultValue = null) {
    try {
      const value = localStorage.getItem(key)
      return value ? JSON.parse(value) : defaultValue
    } catch (error) {
      console.error('localStorage读取失败:', error)
      return defaultValue
    }
  }

  // 删除localStorage
  static removeLocal(key) {
    try {
      localStorage.removeItem(key)
    } catch (error) {
      console.error('localStorage删除失败:', error)
    }
  }

  // 清空localStorage
  static clearLocal() {
    try {
      localStorage.clear()
    } catch (error) {
      console.error('localStorage清空失败:', error)
    }
  }
}

// 日期工具类
export class DateUtil {
  // 格式化日期
  static format(date, format = 'YYYY-MM-DD HH:mm:ss') {
    const d = new Date(date)
    const year = d.getFullYear()
    const month = String(d.getMonth() + 1).padStart(2, '0')
    const day = String(d.getDate()).padStart(2, '0')
    const hour = String(d.getHours()).padStart(2, '0')
    const minute = String(d.getMinutes()).padStart(2, '0')
    const second = String(d.getSeconds()).padStart(2, '0')

    return format
      .replace('YYYY', year)
      .replace('MM', month)
      .replace('DD', day)
      .replace('HH', hour)
      .replace('mm', minute)
      .replace('ss', second)
  }

  // 获取相对时间
  static relative(date) {
    const now = new Date()
    const target = new Date(date)
    const diff = now - target

    const minute = 60 * 1000
    const hour = 60 * minute
    const day = 24 * hour

    if (diff < minute) {
      return '刚刚'
    } else if (diff < hour) {
      return `${Math.floor(diff / minute)}分钟前`
    } else if (diff < day) {
      return `${Math.floor(diff / hour)}小时前`
    } else {
      return `${Math.floor(diff / day)}天前`
    }
  }
}
